import random

import pygame.sprite

from actor.mine_boom import Mine, Boom
from dialog.tunnel_dialog import TunnelTask2BeginDialog, TunnelTask2EndDialog, TunnelTaskFailDialog
from scene.task.stage_one import BaseTask, TaskStatus

"""
击杀日军任务类
"""


class KillJapaneseTask(BaseTask):
    def __init__(self, xiao_tie, japanese, font):
        """
        任务类
        :param xiao_tie:
        :param japanese:
        :param font:
        """
        self.sound2 = pygame.mixer.Sound('resources/sound/升级音效.mp3')
        self.japanese = japanese
        self.xiao_tie = xiao_tie
        self.mines = pygame.sprite.Group()
        self.booms = pygame.sprite.Group()
        self.score = 0
        mine = pygame.image.load("./resources/images/mine/mine.png")
        self.mine_image = pygame.transform.scale(mine, (40, 40))
        self.boom_image = pygame.image.load("./resources/images/mine/boom.png")
        self.font = font
        self.sound_boom = pygame.mixer.Sound('resources/sound/爆炸声.wav')
        self.task_dialog = None
        self.win_dialog = None
        self.fail_dialog = None
        self.__init_dialog()
        super(KillJapaneseTask, self).__init__(self.task_dialog, self.win_dialog,
                                               self.fail_dialog)

    def __init_dialog(self):
        """
        对话初始化
        :return:
        """
        self.task_dialog = TunnelTask2BeginDialog(self.font)
        self.win_dialog = TunnelTask2EndDialog(self.font)
        self.fail_dialog = TunnelTaskFailDialog(self.font)

    def __task_need(self, collisions):
        """
        任务初始化
        :param collisions:
        :return:
        """
        for wall in collisions:
            self.sound_boom.play()
            self.create_boom(wall.rect.x - wall.rect.width,
                             wall.rect.y - wall.rect.height)
            self.score += 1
            if self.score >= 2:
                self.task_status = TaskStatus.win_dialog
                self.xiao_tie.set_status("army")
                self.sound2.play()

    def do_collide_group(self):
        """
        实现组与组碰撞检测
        :return:
        """
        is_collide = pygame.sprite.spritecollide(self.xiao_tie, self.japanese, False)
        if is_collide:
            if self.xiao_tie.time_stamp == 0:
                # 受到伤害
                self.xiao_tie.hp -= random.randint(30, 50)
                # 增加无敌帧设定
                self.xiao_tie.time_stamp = 5
            else:
                # 无敌帧减少
                self.xiao_tie.time_stamp -= 1
            # 任务结束条件
            if self.xiao_tie.hp <= 0:
                self.task_status = TaskStatus.fail_dialog

    def do_self_task(self, key_down, key_list):
        """
        具体任务内容初始化
        :param key_down:
        :param key_list:
        :return:
        """
        self.do_collide_group()
        collisions = pygame.sprite.groupcollide(self.mines, self.japanese, True, True)
        self.__task_need(collisions)
        # 埋地雷动作
        if key_down and key_list[pygame.K_SPACE]:
            if self.xiao_tie.boom_count > 0:
                self.create_mine(self.xiao_tie.pos_x, self.xiao_tie.pos_y)
                self.xiao_tie.boom_count -= 1

    def create_boom(self, x, y):
        """
        实现爆炸操作
        :param x:
        :param y:
        :return:
        """
        mine = Boom(x, y, self.boom_image, 4, 3)
        self.booms.add(mine)

    def create_mine(self, x, y):
        """
        实现埋地雷操作
        :param x:
        :param y:
        :return:
        """
        mine = Mine(x, y, self.mine_image)
        self.mines.add(mine)

    def draw_task(self, current_surface, x, y):
        """
        绘制任务相应内容
        :param current_surface:
        :param x:
        :param y:
        :return:
        """
        # 画地雷
        for mine in self.mines:
            mine.draw(current_surface, x, y)
        # 画爆炸
        for boom in self.booms:
            boom.draw(current_surface, x, y)
